#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年3月8日

@author: Administrator
'''
from account import settings
from account.models import Profile
from common.decorators import login_required
from common.utils import tool
from django.contrib.auth.models import User
from django.db.models.query_utils import Q
from django.http.response import HttpResponse
from django.template.response import TemplateResponse
from funcresource.decorators import user_funcresource
from icon.models import Icon
import copy
import ujson as json


@login_required
@user_funcresource
def index(request, template="icon/admin/icon/iconlist.html"):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    return TemplateResponse(request, template, rsp_data)
    
@login_required
def list(request):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    rsp_data["data"] = []
    start = int(request.POST['start'])
    length = int(request.POST['length'])
    keyword = request.POST["search[value]"]
    draw = request.POST['draw']
    count = Icon.objects.filter(Q(name__icontains=keyword)).count()
    icons = Icon.objects.filter(Q(name__icontains=keyword))[start:start + length]
    for icon in icons:
        record = {
            "id":icon.id,
            "name":icon.name,
            "remark":icon.remark,
            "css_class":icon.css_class,
        }
        rsp_data["data"].append(record)
    rsp_data["recordsTotal"] = count
    rsp_data["recordsFiltered"] = count
    rsp_data["draw"] = draw
    return HttpResponse(json.dumps(rsp_data), mimetype='application/json')

@login_required
def icon_all(request):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    rsp_data["data"] = {}
    rsp_data["data"]["icons"] = []
    icons = Icon.objects.all()
    for icon in icons:
        record = {
            "id":icon.id,
            "name":icon.name,
        }
        rsp_data["data"]["icons"].append(record)
    return HttpResponse(json.dumps(rsp_data), mimetype='application/json')
        
